/**
 * 给定一个包含非负整数的 m x n 网格 grid ，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。
 * <p>
 * 说明：每次只能向下或者向右移动一步。
 * <p>
 * 输入：grid = [[1,3,1],[1,5,1],[4,2,1]]
 * 输出：7
 * 解释：因为路径 1→3→1→1→1 的总和最小。
 * <p>
 * 输入：grid = [[1,2,3],[4,5,6]]
 * 输出：12
 */
class Solution {

    public static void main(String[] args) {
        int[][] grid = new int[][]{
                {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, {1, 3, 1}, {1, 5, 1}, {4, 2, 1}
                // {1, 2, 3}, {4, 5, 6}
        };
        long s = System.nanoTime();
        System.out.println(minPathSum(grid));
        System.out.println(System.nanoTime() - s);
    }

    /**
     * 用递归去做是最快的
     *
     * @param grid
     * @return
     */
    public static int minPathSum(int[][] grid) {
        int x = grid[0].length, y = grid.length;
        int[][] arr = new int[y][x];
        return helper(grid, arr, 0, 0, y - 1, x - 1);
    }

    public static int helper(int[][] grid, int[][] arr, int i, int j, int h, int l) {
        if (i == h && j == l) {
            return grid[h][j];
        }
        if (arr[i][j] != 0) {
            return arr[i][j];
        } else {
            if (i + 1 == arr.length) {
                arr[i][j] = grid[i][j] + helper(grid, arr, i, j + 1, h, l);
                return arr[i][j];
            }
            if (j + 1 == arr[0].length) {
                arr[i][j] = grid[i][j] + helper(grid, arr, i + 1, j, h, l);
                return arr[i][j];
            }
            arr[i][j] = grid[i][j] + Math.min(helper(grid, arr, i, j + 1, h, l), helper(grid, arr, i + 1, j, h, l));
            return arr[i][j];
        }
    }

    /**
     * 可以先处理边边角角，这样子可以减少比较次数
     *
     * @param grid
     * @return
     */
    public static int minPathSum3(int[][] grid) {
        int x = grid[0].length, y = grid.length;
        int[][] dp = new int[y][x];
        dp[0][0] = grid[0][0];
        for (int i = 1; i < x; i++) {
            dp[0][i] = dp[0][i - 1] + grid[0][i];
        }
        for (int i = 1; i < y; i++) {
            dp[i][0] = dp[i - 1][0] + grid[i][0];
        }
        for (int i = 1; i < y; i++) {
            for (int j = 1; j < x; j++) {
                dp[i][j] = grid[i][j] + Math.min(dp[i][j - 1], dp[i - 1][j]);
            }
        }
        return dp[y - 1][x - 1];
    }

    /**
     * 可以用一个一维表来存即可
     *
     * @param grid
     * @return
     */
    public static int minPathSum2(int[][] grid) {
        int x = grid[0].length, y = grid.length;
        int[] dp = new int[x];
        int tmp = 0;
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                if (j > 0) {
                    tmp = Math.min(tmp, dp[j - 1]);
                }
                if (i > 0) {
                    tmp = Math.min(tmp, dp[j]);
                }
                tmp += grid[i][j];
                dp[j] = tmp;
                tmp = Integer.MAX_VALUE;
            }
        }
        return dp[x - 1];
    }

    /**
     * 肯定也是走动态规划，用一张dp表存之前的和
     *
     * @param grid
     * @return
     */
    public static int minPathSum1(int[][] grid) {
        int x = grid[0].length, y = grid.length;
        int[][] dp = new int[y][x];
        int tmp = 0;
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                if (j > 0) {
                    tmp = Math.min(tmp, dp[i][j - 1]);
                }
                if (i > 0) {
                    tmp = Math.min(tmp, dp[i - 1][j]);
                }
                tmp += grid[i][j];
                dp[i][j] = tmp;
                tmp = Integer.MAX_VALUE;
            }
        }
        return dp[y - 1][x - 1];
    }
}